package wowraidmanager.client.wishlist.top;

import java.util.ArrayList;

import wowraidmanager.client.BaseComposite;
import wowraidmanager.shared.model.wishlist.Droplist;
import wowraidmanager.shared.model.wishlist.Member;
import wowraidmanager.shared.model.wishlist.RaidbossDroplist;
import wowraidmanager.shared.model.wishlist.UserStatus;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;

public class TopBossInfo extends BaseComposite{

    private static TopBossInfoUiBinder uiBinder = GWT.create(TopBossInfoUiBinder.class);

    String raidzone;

    UserStatus userStatus;

    ArrayList<Member> guildMember;

    ArrayList<RaidbossDroplist> raidzoneDroplist;

    @UiField
    FlexTable guildwishlist;

    @UiField
    ListBox filterListBox;

    @UiField
    ListBox modeListBox;

    interface TopBossInfoUiBinder extends UiBinder<Widget, TopBossInfo>{
    }

    public TopBossInfo(String raidzone, UserStatus userStatus, ArrayList<RaidbossDroplist> raidzoneDroplist, ArrayList<Member> guildMember){
        initWidget(uiBinder.createAndBindUi(this));
        this.raidzone = raidzone;
        this.userStatus = userStatus;
        this.raidzoneDroplist = raidzoneDroplist;
        this.guildMember = guildMember;

        setGuildwishlist();
        setListBox();
    }

    private void setListBox(){
        filterListBox.addChangeHandler(new ChangeHandler(){

            public void onChange(ChangeEvent event){
                setGuildwishlist();

            }
        });

        modeListBox.addChangeHandler(new ChangeHandler(){

            public void onChange(ChangeEvent event){
                setGuildwishlist();
            }
        });
    }


    protected void setGuildwishlist(){
        int row = 0;
        guildwishlist.removeAllRows();
        String filter = filterListBox.getValue(filterListBox.getSelectedIndex());
        String mode = modeListBox.getValue(modeListBox.getSelectedIndex());

        if(mode.equals("item")){
            if(filter.equals("both")){
                row = setBossDroplist(row, false, "Normal");
                setBossDroplist(row, true, "Heroic");
            }else if(filter.equals("normal")){
                setBossDroplist(row, false, "Normal");
            }else if(filter.equals("heroic")){
                setBossDroplist(row, true, "Heroic");
            }
        }else if(mode.equals("person")){
            if(filter.equals("both")){
                row = setPersonDroplist(row, false, "Normal");
                setPersonDroplist(row, true, "Heroic");
            }else if(filter.equals("normal")){
                setPersonDroplist(row, false, "Normal");
            }else if(filter.equals("heroic")){
                setPersonDroplist(row, true, "Heroic");
            }
        }else if(mode.equals("personAlt")){
            if(filter.equals("both")){
                row = setPersonAltDroplist(row, false, "Normal");
                setPersonAltDroplist(row, true, "Heroic");
            }else if(filter.equals("normal")){
                setPersonAltDroplist(row, false, "Normal");
            }else if(filter.equals("heroic")){
                setPersonAltDroplist(row, true, "Heroic");
            }
        }
    }

    private int setPersonDroplist(int row, boolean isHeroic, String mode){
        int firstrow = row;
        row++;
        guildwishlist.setText(row, 0, mode);
        guildwishlist.getCellFormatter().setHorizontalAlignment(row, 0, HasHorizontalAlignment.ALIGN_CENTER);
        guildwishlist.getCellFormatter().setStyleName(row, 0, "table-header");

        row = firstrow + 2;
        int column = 0;
        for(RaidbossDroplist droplist:raidzoneDroplist){
            String setHtml = "";
            guildwishlist.setHTML(firstrow, column, "<a href=\"http://www.wowhead.com/npc=" + droplist.getBossId() + "\" target=\"_blank\">" + droplist.getBossName() + "</a>");
            for(Member member:guildMember){
                if(droplist.getDroplist() != null){
                    if(member.getWishlist() != null){
                        for(Droplist currentwishlist:member.getWishlist()){
                            if(currentwishlist.getBossName().equals(droplist.getBossName()) && currentwishlist.isHeroic() == isHeroic){
                                setHtml += member.getMemberName() + "<br/>";
                                break;
                            }
                        }
                    }
                }
            }
            if(setHtml.equals("")){
                guildwishlist.setHTML(row, column, "&nbsp;");
            }else{
                guildwishlist.setHTML(row, column, setHtml);
                guildwishlist.getCellFormatter().setVerticalAlignment(row, column, HasVerticalAlignment.ALIGN_TOP);
            }
            column++;
        }

        guildwishlist.getFlexCellFormatter().setColSpan(firstrow + 1, 0, guildwishlist.getCellCount(0));
        return guildwishlist.getRowCount() + 1;
    }

    private int setPersonAltDroplist(int row, boolean isHeroic, String mode){
        int firstrow = row;
        guildwishlist.setText(row, 0, mode);
        guildwishlist.getCellFormatter().setHorizontalAlignment(row, 0, HasHorizontalAlignment.ALIGN_CENTER);
        guildwishlist.getCellFormatter().setStyleName(row, 0, "table-header");

        row++;
        int column = 0;
        for(RaidbossDroplist droplist:raidzoneDroplist){
            String setHtml = "";
            guildwishlist.setHTML(row, column, "<a href=\"http://www.wowhead.com/npc=" + droplist.getBossId() + "\" target=\"_blank\">" + droplist.getBossName() + "</a>");
            for(Member member:guildMember){
                if(droplist.getDroplist() != null){
                    if(member.getWishlist() != null){
                        for(Droplist currentwishlist:member.getWishlist()){
                            if(currentwishlist.getBossName().equals(droplist.getBossName()) && currentwishlist.isHeroic() == isHeroic){
                                setHtml += member.getMemberName() + "<br/>";
                                break;
                            }
                        }
                    }
                }
            }
            column++;
            if(setHtml.equals("")){
                guildwishlist.setHTML(row, column, "&nbsp;");
            }else{
                guildwishlist.setHTML(row, column, setHtml);
                guildwishlist.getCellFormatter().setVerticalAlignment(row, column, HasVerticalAlignment.ALIGN_TOP);
            }
            column = 0;
            row++;
        }

        guildwishlist.getFlexCellFormatter().setColSpan(firstrow, 0, guildwishlist.getCellCount(1));
        return guildwishlist.getRowCount() + 1;
    }

    private int setBossDroplist(int row, boolean isHeroic, String mode){

        int firstrow = row;
        row++;
        guildwishlist.setText(row, 0, mode);
        guildwishlist.getCellFormatter().setHorizontalAlignment(row, 0, HasHorizontalAlignment.ALIGN_CENTER);
        guildwishlist.getCellFormatter().setStyleName(row, 0, "table-header");
        row++;
        for(Member member:guildMember){
            Anchor memberAnchor = new Anchor(member.getMemberName());
            memberAnchor.setTarget("_blank");
            memberAnchor.setHref("http://us.battle.net/wow/en/character/" + member.getRealm() + "/" + member.getMemberName() + "/advanced");
            guildwishlist.setWidget(row, 0, memberAnchor);
            guildwishlist.getFlexCellFormatter().setHorizontalAlignment(row, 0, HasHorizontalAlignment.ALIGN_LEFT);
            row++;
        }

        row = firstrow + 2;
        int column = 1;
        for(RaidbossDroplist droplist:raidzoneDroplist){
            guildwishlist.setHTML(firstrow, column, "<a href=\"http://www.wowhead.com/npc=" + droplist.getBossId() + "\" target=\"_blank\">" + droplist.getBossName() + "</a>");
            for(Member member:guildMember){
                String setHtml = "";
                if(droplist.getDroplist() != null){
                    for(Droplist dropitem:droplist.getDroplist()){
                        if(member.getWishlist() != null){
                            for(Droplist currentwishlist:member.getWishlist()){
                                if(currentwishlist.getId().equals(dropitem.getId()) && currentwishlist.getBossName().equals(dropitem.getBossName()) && currentwishlist.isHeroic() == isHeroic){
                                    setHtml += "<a href=\"http://www.wowhead.com/item=" + currentwishlist.getId() + "\" rel=\"item=" + currentwishlist.getId() + "\" class=\"q" + (Integer.valueOf(currentwishlist.getItemColor()) + 1) + "\" target=\"_blank\">[" + currentwishlist.getItemName() + "]</a><br/>";
                                }
                            }
                        }
                    }
                }
                if(!setHtml.equals("")){
                    guildwishlist.setHTML(row, column, setHtml);
                    guildwishlist.getCellFormatter().setStyleName(row, column, "itemlink");
                }else{
                    guildwishlist.setHTML(row, column, "&nbsp;");
                }
                row++;
            }
            column++;
            row = firstrow + 2;
        }

        guildwishlist.getFlexCellFormatter().setColSpan(firstrow + 1, 0, guildwishlist.getCellCount(0));
        return guildwishlist.getRowCount() + 1;
    }

}
